package com.itheima.tlias.controller;


import com.itheima.tlias.pojo.Result;
import com.itheima.tlias.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

@RestController
@Slf4j
public class UploadController {
	@Autowired
	private AliyunOSSOperator aliyunOSSOperator;

	@PostMapping("/upload")
	public Result upload(MultipartFile file) throws Exception {
		//String targetFileName = null;
		//if (!file.isEmpty()) {
		//	String fileName = file.getOriginalFilename();
		//	if (fileName != null) {
		//		String extFile = fileName.substring(fileName.lastIndexOf("."));
		//		targetFileName = UUID.randomUUID().toString().replaceAll("-", "") + extFile;
		//		String targetPath = "D:/images/" + targetFileName;
		//		File targetFile = new File(targetPath);
		//		file.transferTo(targetFile);
		//	}
		//}
		//log.info("上传文件{}", file);
		//return Result.success("targetFileName：" + targetFileName);

		log.info("上传文件{}", file);
		if (file != null) {
			String url = aliyunOSSOperator.upload(file.getBytes(), Objects.requireNonNull(file.getOriginalFilename()));
			return Result.success(url);
		}
		return Result.error("上传失败");
	}
}
